package org.javabb.infra;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.javabb.vo.Post;
import org.javabb.vo.PostFile;

import com.opensymphony.webwork.dispatcher.multipart.MultiPartRequestWrapper;

public class PostSupport {

	/**
	 * Support upload files by WebWork's action
	 * 
	 * @param servlet
	 * @return
	 * @throws Exception
	 */
	public static Set uploadPostFiles(Post post, HttpServletRequest req) throws Exception {

		MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper)req;
		HashMap hm = FileTransfer.uploadFileRecursive(multiWrapper);
		Set setPostFiles = new HashSet();
		String pathName = FileTransfer.getAbsolutPathName((MultiPartRequestWrapper)req);
		Iterator i = hm.keySet().iterator();
		int cnt = 0;
		while (i.hasNext()) {
			String fileName = (String) i.next();
			String fileSize = hm.get(fileName).toString();

			PostFile pFile = new PostFile();
			pFile.setFileName(fileName);
			pFile.setFileSize(fileSize);
			pFile.setFilePath(pathName);
			//pFile.setUserFileName(fileNames[cnt]);
			pFile.setPost(post);

			setPostFiles.add(pFile);
			cnt ++;
		}

		return setPostFiles;
	}

}
